﻿/*
 * Developer: Joao Palma
 * Empresa: Anaprog
 * Data: 05-01-2010
 * Hora: 17:48
 */

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace Contactos
{
    public partial class frmPrincipal : Form
    {
        #region "Declarações"
        public static SqlConnection conn = new SqlConnection();
        DataSet _ds;
        DataView _dv;
        DataTable _dt;
        SqlDataAdapter _da;
        SqlCommandBuilder _builder;
        #endregion

        public frmPrincipal()
        {
            InitializeComponent();
            Text = Application.ProductName + " " + Application.ProductVersion;
            SetStyle();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conn.ConnectionString = Properties.Settings.Default.ContactosConnectionString;
            try
            {                
                conn.Open();
                conn.Close();
            }
            catch
            {
                frmSql sql = new frmSql();
                sql.ShowDialog();
            }

            _ds = new DataSet("ListaContactos") { EnforceConstraints = false };
            _da = new SqlDataAdapter("select * from CONTACTOS", conn);
            _dt = new DataTable();
            _ds.Tables.Add(_dt);
            _da.Fill(_dt);
            _dv = new DataView(_dt);

            _builder = new SqlCommandBuilder(_da);
            _da.InsertCommand = _builder.GetInsertCommand();

            GridContactos.DataSource = _dv;
            GridContactos.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            GridContactos.Columns["Nome"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            GridContactos.Columns["Numero"].Visible = false;
            GridContactos.Columns["Telefone Emprego"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }

        #region Filtro por Nome

        private void txtNome_KeyDown(object sender, KeyEventArgs e)
        { _dv.RowFilter = "Nome like '%" + txtNome.Text + "%'"; }

        #endregion

        public void SetStyle()
        {
            BackColor = Color.FromArgb(228, 234, 230);
            SetBase(228, 234, 230, Color.FromArgb(254, 209, 94));
        }

        #region SetBase

        public void SetBase(int R, int G, int B, Color HaloColor)
        {
            this.SuspendLayout();
            foreach (Control control in this.Controls)
            {
                if (typeof(RibbonStyle.TabStrip) == control.GetType())
                {
                    ((RibbonStyle.TabStripProfessionalRenderer)((RibbonStyle.TabStrip)control).Renderer).HaloColor = HaloColor;
                    ((RibbonStyle.TabStripProfessionalRenderer)((RibbonStyle.TabStrip)control).Renderer).BaseColor = Color.FromArgb(R + 4, G + 3, B + 3);
                    for (int i = 0; i < ((RibbonStyle.TabStrip)control).Items.Count; i++)
                    {
                        RibbonStyle.Tab _tab = (RibbonStyle.Tab)((RibbonStyle.TabStrip)control).Items[i];

                        #region Set Tab Colors
                        if (Color.FromArgb(R, G, B).GetBrightness() < 0.5)
                        {
                            try
                            {
                                _tab.ForeColor = Color.FromArgb(R + 76, G + 71, B + 66);
                            }
                            catch
                            {
                                _tab.ForeColor = Color.FromArgb(250, 250, 250);
                            }
                        }
                        else
                        {
                            try
                            {
                                _tab.ForeColor = Color.FromArgb(R - 96, G - 91, B - 86);
                            }
                            catch
                            {
                                _tab.ForeColor = Color.FromArgb(10, 10, 10);
                            }
                        }
                        #endregion
                    }

                    control.BackColor = Color.FromArgb(R - 10, G - 8, B + 13);

                }
                if (typeof(RibbonStyle.TabPageSwitcher) == control.GetType())
                {
                    control.BackColor = Color.FromArgb(R - 10, G - 8, B + 13);

                    foreach (Control _control in control.Controls)
                    {
                        if (typeof(RibbonStyle.TabStripPage) == _control.GetType())
                        {
                            ((RibbonStyle.TabStripPage)_control).BaseColor = Color.FromArgb(R, G, B);
                            ((RibbonStyle.TabStripPage)_control).BaseColorOn = Color.FromArgb(R, G, B);

                            foreach (Control __control in _control.Controls)
                            {
                                if (typeof(RibbonStyle.TabPanel) == __control.GetType())
                                {
                                    #region Set TabPanel Colors
                                    if (Color.FromArgb(R, G, B).GetBrightness() < 0.5)
                                    {
                                        try
                                        {
                                            __control.ForeColor = Color.FromArgb(R + 76, G + 71, B + 66);
                                        }
                                        catch
                                        {
                                            __control.ForeColor = Color.FromArgb(250, 250, 250);
                                        }
                                    }
                                    else
                                    {
                                        try
                                        {
                                            __control.ForeColor = Color.FromArgb(R - 96, G - 91, B - 86);
                                        }
                                        catch
                                        {
                                            __control.ForeColor = Color.FromArgb(10, 10, 10);
                                        }
                                    }
                                    #endregion

                                    ((RibbonStyle.TabPanel)__control).BaseColor = Color.FromArgb(R, G, B);
                                    ((RibbonStyle.TabPanel)__control).BaseColorOn = Color.FromArgb(R + 6, G + 6, B + 10);

                                    foreach (Control ___control in __control.Controls)
                                    {
                                        if (typeof(RibbonStyle.RibbonButton) == ___control.GetType())
                                        {
                                            ((RibbonStyle.RibbonButton)___control).InfoColor = Color.FromArgb(R, G, B);

                                            RibbonStyle.RibbonButton _but = (RibbonStyle.RibbonButton)___control;

                                            #region Set Button Colors
                                            if (Color.FromArgb(R, G, B).GetBrightness() < 0.5)
                                            {
                                                try
                                                {
                                                    _but.ForeColor = Color.FromArgb(R + 76, G + 71, B + 66);
                                                }
                                                catch
                                                {
                                                    _but.ForeColor = Color.FromArgb(250, 250, 250);
                                                }
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    _but.ForeColor = Color.FromArgb(R - 96, G - 91, B - 86);
                                                }
                                                catch
                                                {
                                                    _but.ForeColor = Color.FromArgb(10, 10, 10);
                                                }
                                            }
                                            #endregion

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.ResumeLayout(false);
        }
        #endregion

        private void ribbonButton1_Click(object sender, EventArgs e)
        {
            Close();
            Application.Exit();
        }

        #region Operações

        private void ribbonButton2_Click(object sender, EventArgs e)//Novo registo
        {
            GridContactos.CurrentCell = GridContactos.Rows[GridContactos.RowCount - 1].Cells[1];
            GridContactos.BeginEdit(false);
        }

        #region Apaga Registo
        private void ribbonButton3_Click(object sender, EventArgs e)
        {
            if (GridContactos.CurrentCell != null)
            {
                GridContactos.Rows.RemoveAt(GridContactos.CurrentRow.Index);
                GridContactos.EndEdit();
            }
        }
        #endregion

        #region Grava Registo
        private void ribbonButton4_Click(object sender, EventArgs e)
        {
            Validate();
            GridContactos.EndEdit();
            _da.Update(_ds.Tables["Contactos"]);
        }
        #endregion

        #region Avança Registo
        private void btNext_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0 &&
                GridContactos.CurrentRow.Index != GridContactos.RowCount - 1)
            { GridContactos.CurrentCell = GridContactos[1, GridContactos.CurrentRow.Index + 1]; }
        }
        #endregion

        private void btPrevious_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0 &&
               GridContactos.CurrentRow.Index != 0)
            { GridContactos.CurrentCell = GridContactos[1, GridContactos.CurrentRow.Index - 1]; }
        }

        private void btFirst_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0)
            { GridContactos.CurrentCell = GridContactos[1, 0]; }
        }

        private void btLast_Click(object sender, EventArgs e)
        {
            if (GridContactos.RowCount > 0)
            { GridContactos.CurrentCell = GridContactos[1, GridContactos.RowCount - 1]; }
        }

        #endregion
    }
}
